package com.xiaode.controller.app.teacher.student;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class StudentService {
    /**
     * 展示学生信息
     *
     * @param adminId
     * @param date
     * @return
     */
    public Ret exhibitionStudent(Integer adminId, Date date) throws Exception {
        //查询与当前教师结对的学生信息
        List<Record> students = findStudent(adminId);
        Ret ret = statisticsStudent(students, date);
        ret.set("students", students);
        return ret;
    }

    /**
     * 查询当前教师管理的所有学生信息
     *
     * @param adminId
     * @return
     */
    public List<Record> findStudent(Integer adminId) throws Exception {
        Kv kv = Kv.by("adminId", adminId);
        List<Record> records = Db.template("findStudentInfo", kv).find();
        return records;
    }

    /**
     * 统计学生信息
     */
    public Ret statisticsStudent(List<Record> students, Date date) {
        //获取日期的 日 周 月
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = date.toInstant().atZone(zoneId).toLocalDateTime();
        //获得周
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("w");
        int week = Integer.parseInt(localDateTime.format(dateTimeFormatter)) - 1;
        ArrayList<String> dayUnCommitList = new ArrayList<>();
        ArrayList<String> dayCommitList = new ArrayList<>();
        ArrayList<String> weekUnCommitList = new ArrayList<>();
        ArrayList<String> weekCommitList = new ArrayList<>();
        ArrayList<String> monthUnCommitList = new ArrayList<>();
        ArrayList<String> monthCommitList = new ArrayList<>();
        ArrayList<Kv> lineChartList = new ArrayList<>();
        for (Record student : students) {
            Integer id = student.getInt("id");
            String name = student.getStr("name");
            Integer monthUnCommit = 0;
            //月集合
            if (monthUnCommit > 0) {
                monthCommitList.add(name);
            } else {
                monthUnCommitList.add(name);
            }

            //折线图数据
            Kv kv = lineChartInfo(id, name, week);
            lineChartList.add(kv);
        }
        Kv dayCommitKv = Kv.by("value", dayCommitList.size()).set("stu", dayCommitList).set("name", "已提交");
        Kv weekCommitKv = Kv.by("value", weekCommitList.size()).set("stu", weekCommitList).set("name", "已提交");
        Kv monthCommitKv = Kv.by("value", monthCommitList.size()).set("stu", monthCommitList).set("name", "已提交");
        Kv dayUnCommitKv = Kv.by("value", dayUnCommitList.size()).set("stu", dayUnCommitList).set("name", "未提交");
        Kv weekUnCommitKv = Kv.by("value", weekUnCommitList.size()).set("stu", weekUnCommitList).set("name", "未提交");
        Kv monthUnCommitKv = Kv.by("value", monthUnCommitList.size()).set("stu", monthUnCommitList).set("name", "未提交");
        ArrayList<Kv> dayKvs = new ArrayList<>();
        ArrayList<Kv> weekKvs = new ArrayList<>();
        ArrayList<Kv> monthKvs = new ArrayList<>();
        Collections.addAll(dayKvs, dayCommitKv, dayUnCommitKv);
        Collections.addAll(weekKvs, weekCommitKv, weekUnCommitKv);
        Collections.addAll(monthKvs, monthCommitKv, monthUnCommitKv);
        Ret ret = Ret.ok().set("day", dayKvs).set("week", weekKvs).set("month", monthKvs).set("lineChartList", lineChartList);
        return ret;
    }


    /**
     * 学生折线图数据
     */
    public Kv lineChartInfo(Integer stuId, String name, Integer week) {
        Kv kv = Kv.by("studentId", stuId).set("week", week);
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime monday = localDateTime.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime sunday = localDateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusDays(1).withHour(23).withMinute(59).withSecond(59);
        kv.set("monday",monday).set("sunday",sunday);
        List<Record> records = Db.template("lineChartStudent", kv).find();
        ArrayList<Integer> list = new ArrayList<>(7);
        int num = 0;
        for (Record record : records) {
            LocalDateTime addTime = record.getLocalDateTime("addt");
            int value = addTime.getDayOfWeek().getValue();
            if (value == 7){
                continue;
            }
            int oldScore = record.getInt("oldScore");
            for (int i = num; i <= value - 1; i++) {
                if (i == value - 1) {
                    list.add(record.getInt("score"));
                    num = i + 1;
                } else {
                    list.add(oldScore);
                }
            }
        }
        if (list.size() > 0) {
            //如果当前时间大于最近分值变化时间
            while (list.size() < LocalDateTime.now().getDayOfWeek().getValue()) {
                list.add(list.get(list.size() - 1));
            }
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("w");
        int curWeek = Integer.parseInt(LocalDateTime.now().format(dateTimeFormatter)) - 1;
        //如果不是当前周的数据 则自动补全
        if (curWeek > week) {
            if (list.size() != 0 && list.size() != 7) {
                int s = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i) != 0) {
                        s = list.get(i);
                    }
                }
                for (int i = list.size(); i < 7; i++) {
                    list.add(s);
                }
            }
        }
        Kv res = Kv.by("name", name).set("data", list);
        return res;
    }

    
}
